package io.github.wslxm.springbootplus2.manage.sys.controller;

import io.github.wslxm.springbootplus2.core.base.controller.BaseController;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.constant.BaseConstant;
import io.github.wslxm.springbootplus2.core.result.Result;
import io.github.wslxm.springbootplus2.core.utils.threadpool.XjThreadUtil;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysNumberDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysNumberQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysNumberVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysNumberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 编号生成表 前端控制器
 *
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>
 *
 * @author ws
 * @email 1720696548@qq.com
 * @date 2023-12-06 11:08:13
 */
@RestController
@RequestMapping(BaseConstant.Uri.API_ADMIN + "/sys/number")
@Tag( name = "编号生成表")
@Slf4j
public class SysNumberController extends BaseController<SysNumberService> {

    @Autowired
    private SysNumberService numberService;

    @GetMapping(value = "/tree")
    @Operation(summary = "树结构数据")
    public Result<List<SysNumberVO>> tree(@ModelAttribute @Validated SysNumberQuery query) {
        return Result.success(baseService.tree(query));
    }

    @GetMapping(value = "/findPage")
    @Operation(summary = "列表查询")
    public Result<BasePage<SysNumberVO>> findPage(@ModelAttribute @Validated SysNumberQuery query) {
        return Result.success(baseService.findPage(query));
    }

    @GetMapping(value = "/{id}")
    @Operation(summary = "ID查询")
    public Result<SysNumberVO> findId(@PathVariable String id) {
        return Result.success(baseService.findId(id));
    }

    @PostMapping
    @Operation(summary = "添加")
    public Result<String> insert(@RequestBody @Validated SysNumberDTO dto) {
        return Result.successInsert(baseService.insert(dto));
    }

    @PutMapping(value = "/{id}")
    @Operation(summary = "ID编辑")
    public Result<Boolean> upd(@PathVariable String id, @RequestBody @Validated SysNumberDTO dto) {
        return Result.successUpdate(baseService.upd(id, dto));
    }

    @DeleteMapping(value = "/{id}")
    @Operation(summary = "ID删除")
    public Result<Boolean> del(@PathVariable String id) {
        return Result.successDelete(baseService.del(id));
    }


    @PostMapping(value = "/generateNumber")
    @Operation(summary = "编号生成测试")
    public Result<String> generateNumber(@RequestParam String code) {
        long startTime = System.currentTimeMillis();
        Set<String> sets = new HashSet<>();
        XjThreadUtil.asyncExecute(() -> {
            for (int i = 0; i < 100; i++) {
                // log.info(redisUtil.getOrderNo("order"));
                String no = numberService.generateNumber(code);
                sets.add(no);
                log.info(no);
            }
            long endTime = System.currentTimeMillis();
            log.info("耗时:{} 当前数据量: {}", (endTime - startTime) / 1000, sets.size());
        });
        XjThreadUtil.asyncExecute(() -> {
            for (int i = 0; i < 100; i++) {
                // log.info(redisUtil.getOrderNo("order"));
                String no = baseService.generateNumber(code);
                sets.add(no);
                log.info(no);
            }
            long endTime = System.currentTimeMillis();
            log.info("耗时:{} 当前数据量: {}", (endTime - startTime) / 1000, sets.size());
        });
//        XjThreadUtil.asyncExecute(() -> {
//            for (int i = 0; i < 100; i++) {
//                // log.info(redisUtil.getOrderNo("order"));
//                sets.add(baseService.generateNumber(code));
//            }
//            long endTime = System.currentTimeMillis();
//            log.info("耗时:{} 当前数据量: {}", (endTime  - startTime) / 1000 ,sets.size());
//        });
//        XjThreadUtil.asyncExecute(() -> {
//            for (int i = 0; i < 100; i++) {
//                // log.info(redisUtil.getOrderNo("order"));
//                sets.add(baseService.generateNumber(code));
//            }
//            long endTime = System.currentTimeMillis();
//            log.info("耗时:{} 当前数据量: {}", (endTime  - startTime) / 1000 ,sets.size());
//        });
//        XjThreadUtil.asyncExecute(() -> {
//            for (int i = 0; i < 100; i++) {
//                // log.info(redisUtil.getOrderNo("order"));
//                sets.add(baseService.generateNumber(code));
//            }
//            long endTime = System.currentTimeMillis();
//            log.info("耗时:{} 当前数据量: {}", (endTime  - startTime) / 1000 ,sets.size());
//        });
        return Result.success(null);
    }
}
